package com.godofball.medtrack.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.godofball.medtrack.common.Result;
import com.godofball.medtrack.pojo.Warehouse;
import com.godofball.medtrack.service.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/warehouse")
@Slf4j
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @GetMapping("/page")
    public Result<Page> page(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             String drugId,
                             String manufacturerId,
                             String purchaseId) {
        log.info("page:{},pageSIze:{}", page, pageSize);

        Page<Warehouse> warehousePage = new Page<>();

        LambdaQueryWrapper<Warehouse> warehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        warehouseLambdaQueryWrapper.eq(StringUtils.hasLength(drugId), Warehouse::getDrugId, drugId);
        warehouseLambdaQueryWrapper.eq(StringUtils.hasLength(manufacturerId), Warehouse::getManufacturerId, manufacturerId);
        warehouseLambdaQueryWrapper.eq(StringUtils.hasLength(purchaseId), Warehouse::getPurchaseId, purchaseId);
        warehouseLambdaQueryWrapper.orderByDesc(Warehouse::getSort).orderByDesc(Warehouse::getUpdateTime);

        warehouseService.page(warehousePage, warehouseLambdaQueryWrapper);

        return Result.success(warehousePage);

    }

    @PutMapping
    public Result<String> update(@RequestBody Warehouse warehouse) {
        log.info("update warehouse:{}",warehouse);

        Warehouse warehouseInfo = new Warehouse();
        warehouseInfo.setId(warehouse.getId());
        warehouseInfo.setSalePrice(warehouse.getSalePrice());
        warehouseInfo.setSort(warehouse.getSort());
        warehouseInfo.setStatus(warehouse.getStatus());

        warehouseService.updateById(warehouseInfo);

        return Result.success(null);
    }

}
